package com.ruoyi.system.listener;

import com.ruoyi.common.modbus.data.MiiHmDeviceData;
import com.ruoyi.common.modbus.data.MiiHmLocationData;
import com.ruoyi.common.modbus.message.MiiMessage;
import com.ruoyi.common.modbus.utils.CacheObjectUtils;
import com.ruoyi.common.modbus.utils.CacheShebeiUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.config.IotModbusServerConfig;
import com.ruoyi.system.domain.IotGateway;
import com.ruoyi.system.domain.IotVariable;
import com.ruoyi.system.domain.IotVariableValue;
import com.ruoyi.system.domain.IotVariableValueTemp;
import com.ruoyi.system.domain.vo.*;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.CellLocationService;
import com.takeoff.iot.modbus.netty.channel.MiiChannel;
import com.takeoff.iot.modbus.netty.data.HeartBeatData;
import com.takeoff.iot.modbus.netty.listener.MiiListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class IotDataLocationListener implements MiiListener {


    private final int dataLength = 8;

    @Autowired
    private IotDeviceMapper iotDeviceMapper;

    @Autowired
    private IotVariableMapper iotVariableMapper;

    @Autowired
    private IotManageEndMapper iotManageEndMapper;

    @Autowired
    private IotEdgeEndMapper iotEdgeEndMapper;

    @Autowired
    private IotVariableValueMapper iotVariableValueMapper;

    @Autowired
    private IotModbusServerConfig iotModbusServerConfig;

    @Autowired
    private IotVariableValueTempMapper iotVariableValueTempMapper;

    @Autowired
    private IotGatewayMapper iotGatewayMapper;

    @Autowired
    private CellLocationService cellLocationService;


    @EventListener
    public void handleReceiveDataEvent(MiiHmLocationData data) {
        log.info("设备基站号: "+ data.getCellId());
        log.info("小区号: "+ data.getLac());
    }

    /**

     * @param channel 消息通道
     * @param message 接收到的设备信息
     */
    @Override
    public synchronized void receive(MiiChannel channel, MiiMessage message) {
        MiiHmLocationData data = (MiiHmLocationData) message.data();
        log.info("定位接受处理数据：" + channel.name() + message.deviceGroup() + data.data());
        String sn = CacheShebeiUtils.get(message.deviceGroup());
//        String sn = CacheObjectUtils.get(channel);
        log.info("定位从机地址：" + message.slaveAddress() + "  SN：" + sn);
        IotGateway iotGateway = iotGatewayMapper.selectIotGatewayDataBySn(sn);
        if(iotGateway == null){
            return;
        }
        log.info("定位api请求1：");
        CellLocationRequest request = new CellLocationRequest();
        log.info("定位api请求基站号："+data.getCellId());
        log.info("定位api请求小区号："+data.getLac());
        if (iotGateway.getGatewayType() == 1) {
            request.setLac(String.valueOf(Integer.parseInt(data.getCellId().replace("\"", ""), 16)));
            request.setCid(String.valueOf(Integer.parseInt(data.getLac().replace("\"", ""), 16)));
        } else {
            request.setLac(String.valueOf(data.getLac()));
            request.setCid(String.valueOf(data.getCellId()));
        }

        CellLocationResponse response = cellLocationService.getCellLocation(request);
        log.info("定位api请求2："+response);
        if (response.getErrcode() == 0){
            log.info("定位接口返回的数据："+response.getAddress());
            iotGateway.setAddr(response.getAddress());
            iotGateway.setLat(new BigDecimal(response.getLat()));
            iotGateway.setLng(new BigDecimal(response.getLon()));
            iotGatewayMapper.updateIotGateway(iotGateway);
        }

    }

    public static void main(String[] args) {
        System.out.println(Integer.parseInt("E34A", 16));
    }

}
